//
//  ClassExt.swift
//  CLProtocolKitSDK
//
//  Created by admin on 2022/3/18.
//

import UIKit

extension customCell{
    open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        scaletominiSelf(obj: self)
    }
    open override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        recoverState(obj: self)
    }
    open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if delegate != nil{
            delegate?.touchEnd()
        }
        recoverState(obj: self)
    }
}

extension customcollectCell{
    open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        scaletominiSelf(obj: self)
    }
    open override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        recoverState(obj: self)
    }
    open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if delegate != nil{
            delegate?.touchEnd()
        }
        recoverState(obj: self)
    }
}

extension customView{
    open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        scaletominiSelf(obj: self)
    }
    open override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        recoverState(obj: self)
    }
    open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        recoverState(obj: self)
        if delegate != nil{
            delegate?.touchEnd()
        }
    }
}

private func scaletominiSelf<T>(obj : T){
    let animation = CABasicAnimation.init(keyPath: "transform.scale")
    animation.fromValue = 1
    animation.toValue = 0.9
    animation.isRemovedOnCompletion = false
    animation.fillMode = .forwards
    animation.autoreverses = false
    animation.duration = 0.15
    if let view = obj as? UIView{
        view.layer.add(animation, forKey: "cell-scale")
    }
}

private func recoverState<T>(obj : T){
    let animation = CABasicAnimation.init(keyPath: "transform.scale")
    animation.fromValue = 0.9
    animation.toValue = 1
    animation.isRemovedOnCompletion = false
    animation.fillMode = .forwards
    animation.autoreverses = false
    animation.duration = 0.1
    if let view = obj as? UIView{
        view.layer.add(animation, forKey: "cell-scale")
    }
}
